package com.bdqn.t382.dao;

import java.sql.*;

public class BaseDao {
    protected Connection conn;  // 连接对象
    protected PreparedStatement pstmt;  // 执行对象
    protected ResultSet rs;  // 结果集对象

    public Connection getConnection() {
        try {
            // 1、加载驱动   ctrl + alt + t
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2、获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/supermarket?serverTimezone=GMT-8&characterEncoding=utf8", "root", "123456");
            //conn = DriverManager.getConnection("jdbc:mysql://www.yuanxiaoxiang.com:3306/ktmall03?serverTimezone=GMT-8&characterEncoding=utf8", "root", "BDQN&123kt!@#");
            System.out.println("数据库" + conn + "连接成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("释放资源中......");
            closeAll(rs, pstmt, conn);
            System.out.println("资源释放完毕");
        }
        return conn;
    }

    // 释放资源
    public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        try {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行增删改的SQL语句
     *
     * @param sql    预编译的SQL语句。  带?点位符
     * @param params 参数数组
     * @return 返回受影响的行数
     */
    public int executeUpdate(String sql, Object... params) {
        conn = getConnection();
        int count = 0;  // 用来保存受影响的行数
        try {
            pstmt = conn.prepareStatement(sql);  // 创建执行对象
            // 为预编译的SQL语句赋值，即替换掉?占位符
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]);
                }
            }
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            closeAll(rs, pstmt, conn);
        }
        return count;
    }

    /**
     * 执行查询的SQL语句
     *
     * @param sql    预编译的SQL语句
     * @param params 参数数组
     * @return 返回结果集ResultSet对象
     */
    public ResultSet executeQuery(String sql, Object... params) {
        conn = getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    //测试连接数据库是否成功
    public static void main(String[] args) {
        BaseDao dao = new BaseDao();
        try {
            dao.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dao.closeAll(dao.rs, dao.pstmt, dao.conn);
        }
    }
}
